package servlet.zyb;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class DoVoteServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		process(req, resp);
	}

	public void process(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("UTF-8");
		String username = (String) req.getSession().getAttribute("username");
		System.out.println(username);
		String optionContent = req.getParameter("optionContent");
		optionContent = new String(optionContent.getBytes("ISO-8859-1"),
				"UTF-8");
		System.out.println("optionContent" + optionContent);
		String qName = (String) req.getSession().getAttribute("qName");
		System.out.println("问题：" + qName);
		SAXBuilder builder = new SAXBuilder();

		Document document = null;
		try {
			document = builder.build(new File(
					"E:\\MyeclipseWorkspace\\vote\\question.xml"));
		} catch (JDOMException e) {
			e.printStackTrace();
		}

		Element root = document.getRootElement();
		List<Element> list = root.getChildren();
		for (int i = 0; i < list.size(); i++) {
			Element question = list.get(i);
			String questionname = question.getChild("questionname").getText();
			System.out.println("xml问题名:" + questionname);
			if (questionname.equals(qName)) {
				Element user = list.get(i).getChild("users");// 这个user是question的子元素
				System.out.println("users:" + user);
				if (user == null || "".equals(user)) {
					Element users = new Element("users");
					Element u = new Element(username);
					u.setText(optionContent);
					users.addContent(u);
					question.addContent(users);
					req.setAttribute("Mes", "投票成功");

					System.out.println("执行成功");
				} else if (user != null) {
					
					List<Element> us = user.getChildren();
					int n =0;
					for(int j=0;j<us.size();j++){
						if(us.get(j).getName().equals(username)){
							req.setAttribute("Mes", "投票失败，您已经投过票了！");
						}
						else{
							n++;
						}
					}
					if(n==us.size()){
						Element u = new Element(username);
						u.setText(optionContent);
						user.addContent(u);
						req.setAttribute("Mes", "投票成功");
					}		
				
				}
			}
		}
		XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());

		FileOutputStream fos = new FileOutputStream(
				"E:\\MyeclipseWorkspace\\vote\\question.xml");
		out.output(document, fos);
		fos.close();

		req.getRequestDispatcher("voteResultMes.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		process(req, resp);
	}
}
